<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if (sxREQ("reqtype") == "save"){
	$errMsg .= subjectAreaMod();
	$dataArea = sxREQUEST("data_area");
	$subjectArea = sxREQUEST("subject_area");
	$newSubjectArea = sxREQUEST("new_subject_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
} elseif (sxREQ("reqtype") == "delete"){
	$errMsg .= subjectAreaDel();
	$dataArea = sxREQUEST("data_area");
	$subjectArea = sxREQUEST("subject_area");
	$newSubjectArea = sxREQUEST("new_subject_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	
} else {
	if(sxREQ("da") == ''){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("missing variable 'da'")); 
		serverRedir($url);
	}
	if(sxREQ("sa") == ''){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("missing variable 'sa'")); 
		serverRedir($url);
	}
	$dataArea = sxREQ('da');
	$subjectArea = $newSubjectArea = sxREQ('sa');
	$sSQL = "SELECT * FROM subject_area WHERE data_area = '$dataArea' AND subject_area = '$subjectArea'";
	$objSDAO->query($sSQL,"mod");
	if(!$row = $objSDAO->fetch("mod")) {
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("missing variable 'da/sa'")); 
		serverRedir($url);
	}
	$description = $row['description'];
	$note = $row['note'];
	$sSQL = "SELECT entity FROM subject_area_entity " .
			" WHERE data_area = '$dataArea' AND subject_area = '$subjectArea' ";
	$objSDAO->query($sSQL,"saent");
	while($rowSAE=$objSDAO->fetch('saent')) {
		$_REQUEST['sae_'.$rowSAE['entity']] = 'ok';
	}
}
$sSQL = "SELECT entity FROM entity WHERE data_area = '$dataArea' ORDER BY entity ";
$objSDAO->query($sSQL,"ent");
$sSQL = "SELECT description FROM data_area WHERE data_area = '$dataArea'";
$objSDAO->query($sSQL,"mod");
if(!$row = $objSDAO->fetch("mod")) {
	$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("missing variable  'da'")); 
	serverRedir($url);
}
//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			objFrm.submit();
		}catch(e){
			raiseError(e.description, "save new subject area");
		}			
	}
	function confDelSA(){
		if (!confirm("Confirm subject-area drop ?")) {
			return;
		}
		try{
			var objFrm = document.getElementById("objFrm");
			objFrm.reqtype.value = 'delete';
			objFrm.submit();
		}catch(e){
			raiseError(e.description, "save new subject area");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"subjectAreaMod.php\" method=\"POST\" enctype=\"multipart/form-data\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">\n");
echo("<input type=\"hidden\" name=\"data_area\" id=\"data_area\" value=\"$dataArea\">\n");
echo("<input type=\"hidden\" name=\"subject_area\" id=\"subject_area\" value=\"$subjectArea\">\n");
//output comandi
$aCmd = array("conf"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("save data"),
										"text"=>_t("save")
										),
					"dele"=>array("pag"=>"javascript:confDelSA()",
										"title"=>_t("delete subject area"),
										"text"=>_t("delete")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>"cancel operation",
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Subjet-Area</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>"._t("Data-Area")."</td>" .
			"<td class=\"text01\" width='10%'>" .
			"$dataArea - ".$row['description']."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>"._t("Subject-Area code")." (*)</td>" .
			"<td class=\"text01\" width='10%'>" .
			"<input class=\"inpt01\" type=\"text\" name=\"new_subject_area\" id=\"new_subject_area\" maxlength=\"32\" value=\"$newSubjectArea\" size='30'></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")." (*)</td>" .
			"<td class=\"text01\" colspan='1'><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\" colspan='1'><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">$note</textarea></td>" .
		"</tr>\n");
echo("</table>\n");

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"100%\" colspan='4'>"._t('Associated entity')."</td>" .
		"</tr>\n");
echo("<tr>\n");
$nn = 0;
while($rowE=$objSDAO->fetch('ent')) {
	if ($nn == 4) {
		echo("</tr>\n<tr>\n");
		$nn = 0;
	}
	$nn++;
	echo("<td class=\"text01\" width=\"25%\">" .
			"<input type='checkbox' name='sae_".$rowE['entity']."' ".
			((sxREQUEST('sae_'.$rowE['entity'])=='ok')?'CHECKED':'')." value='ok' class='text01'>".
			$rowE['entity']."</td>\n");
}
while($nn < 3) {
	echo("<td class=\"text01\" width=\"25%\">&nbsp;</td>\n");
	$nn++;
}
echo("</tr>\n</table>\n");

echo(commandsHTML($aCmd));
echo("<div id=\"messArea\" class=\"mess01\">$errMsg</div>");
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

function subjectAreaDel(){
	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM subject_area_entity  WHERE data_area='".sxRAS("data_area")."' " .
					" AND subject_area = '".sxRAS("subject_area")."'";	
	$objSDAO->exec($sSQL);
	$sSQL = "DELETE FROM subject_area  WHERE data_area='".sxRAS("data_area")."' " .
					" AND subject_area = '".sxRAS("subject_area")."'";	
	$objSDAO->exec($sSQL);
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit();
}
function subjectAreaMod(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["new_subject_area"] = strtolower($_REQUEST["new_subject_area"]);
	if(ereg("[^a-z0-9_]",$_REQUEST["new_subject_area"]) or $_REQUEST["new_subject_area"]=='')
		$errMsg .= _t("Il codice subject-area deve essere un alfanumerico (lettere numeri e _)").".<br>";
	if(trim($_REQUEST["description"]) == '')
		$errMsg .= _t("missing description")."<br>";
	//existence check
	if($_REQUEST["new_subject_area"] != $_REQUEST["subject_area"]) {
		$sSQL = "SELECT count(*) as num FROM subject_area " .
				" WHERE data_area='".sxRAS("data_area")."' " .
					" AND subject_area = '".sxRAS("new_subject_area")."'";
		$objSDAO->query($sSQL,"mod");
		$row = $objSDAO->fetch("mod");
		if($row["num"] != 0)
			$errMsg .= _t("Subject-Area existing.<br>");
	}
	if($errMsg != ""){ return $errMsg; }
	$sSQL = "UPDATE subject_area SET " .
				" description = '".sxRAS("description")."', " .
				" note = '".sxRAS("note")."'" .
				" WHERE data_area='".sxRAS("data_area")."' " .
					" AND subject_area = '".sxRAS("subject_area")."'";	
	$objSDAO->exec($sSQL);
	$sSQL = "DELETE FROM subject_area_entity " .
			" WHERE data_area='".sxRAS("data_area")."' " .
				" AND subject_area = '".sxRAS("subject_area")."'";
	$objSDAO->exec($sSQL);
	foreach($_REQUEST as $nome => $val) {
		if (substr($nome,0,4)!='sae_' or $val != 'ok') { continue; }
		$sSQL = "INSERT INTO subject_area_entity (data_area, subject_area, entity)" .
				" VALUES('".sxRAS("data_area")."', '".sxRAS("subject_area")."', '".
				substr($nome,4)."')";
		$objSDAO->exec($sSQL);
	}
	if($_REQUEST["new_subject_area"] != $_REQUEST["subject_area"]) {
		$sSQL = "UPDATE subject_area SET subject_area = '".sxRAS("new_subject_area")."'" .
				" WHERE data_area='".sxRAS("data_area")."' " .
					" AND subject_area = '".sxRAS("subject_area")."'";
		$objSDAO->exec($sSQL);
		$sSQL = "UPDATE subject_area_entity SET subject_area = '".sxRAS("new_subject_area")."'" .
				" WHERE data_area='".sxRAS("data_area")."' " .
					" AND subject_area = '".sxRAS("subject_area")."'";
		$objSDAO->exec($sSQL);
	}
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit;
}
?>
